package com.winai.launcher.BroadcastReceiver;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.text.TextUtils;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.ks.projectbasictools.utils.LogUtils;
import com.winai.launcher.base.db.DB_Common;
import com.winai.launcher.bean.LiveDataBean;
import com.winai.launcher.common.GlobalPublisher;
import com.winai.launcher.constant.Constant;
import com.winai.launcher.manager.DeviceInfoManager;
import com.winai.launcher.manager.LiveManager;
import com.winai.launcher.utils.NotifyReceiverUtils;

public class AiyishengReceiver extends BroadcastReceiver {
    private Context mContext;
    private final String action_aiyisheng_logout = "com.aiyisheng.speaker.action.logout";//退出登录
    private final String action_pulldown = "com.winai.launcher.action.pulldown.launcher";//禁止或允许下拉状态栏
    private final String ACTION_LIVE_NOTICE = "com.aiyisheng.speaker.action.live_notice";//直播信息广播

    public void init(Context mContext) {
        LogUtils.i("注册了按键广播");
        this.mContext = mContext;

        IntentFilter filter = new IntentFilter();
        filter.addAction(action_aiyisheng_logout);
        filter.addAction(action_pulldown);
        filter.addAction(ACTION_LIVE_NOTICE);
        filter.addAction(Constant.ACTION_USER_INFO);
        mContext.registerReceiver(this, filter);
    }

    @Override
    public void onReceive(Context context, Intent intent) {
        LogUtils.d("监听到艾益生发送过来的广播，intent.getAction() = " + intent.getAction());
        if (intent.getAction() != null && intent.getAction().equals(action_aiyisheng_logout)) {
            LogUtils.d("监听到退出登录广播");
            GlobalPublisher.Notify(GlobalPublisher.LOGOUT);
            // 更新设备激活状态
            DeviceInfoManager.getInstance(context).updateUnbind();
            //说明：禁止或允许下拉状态栏
//            NotifyReceiverUtils.notifyPulldownReceiver(context.getApplicationContext(), false);
        } else if (intent.getAction() != null && intent.getAction().equals(action_pulldown)) {
            boolean enable = intent.getBooleanExtra("enable", false);
            LogUtils.d("接收到艾益生控制下拉栏广播，enable = " + enable);
            if (enable) {
                NotifyReceiverUtils.notifyPulldownReceiver(context.getApplicationContext(), true);
//                NotifyReceiverUtils.notifyToggleWakeUpReceiver(context.getApplicationContext(), true);
            } else {
                NotifyReceiverUtils.notifyPulldownReceiver(context.getApplicationContext(), false);
//                NotifyReceiverUtils.notifyToggleWakeUpReceiver(context.getApplicationContext(), false);
            }
        } else if (intent.getAction() != null && intent.getAction().equals(ACTION_LIVE_NOTICE)) {
            int liveState = intent.getIntExtra("liveState", Constant.LIVE_STATE);
            String routerPath = intent.getStringExtra("routerPath");//直播进入路径
            String data = intent.getStringExtra("data");//直播数据-JSON字符串
            LogUtils.d("接收到艾益生直播通知广播:liveState = " + liveState + ";routerPath = " + routerPath + ";data = " + data);
            LiveDataBean bean = new Gson().fromJson(data, new TypeToken<LiveDataBean>() {
            }.getType());
            LiveManager.getInstance(mContext).setData(liveState, routerPath, bean);
        } else if (intent.getAction() != null && intent.getAction().equals(Constant.ACTION_USER_INFO)) {
            int levelId = intent.getIntExtra("levelId", 0);
            LogUtils.d("接收到艾益生用户信息广播:levelId = " + levelId);
            DB_Common dbCommon = new DB_Common(mContext);
            if (levelId == 1) {
                dbCommon.add("isShowTrain", "false");
            } else {
                dbCommon.add("isShowTrain", "true");
            }
            notifyReceiver(context);
        }
    }

    private void notifyReceiver(Context context) {
        NotifyReceiverUtils.notifyUserInfo(context);
    }
}
